package com.deyuanyun.pic.docmger.service.impl;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.docmger.controller.dto.DocPipelineDTO;
import com.deyuanyun.pic.docmger.controller.dto.DocPipelineQueryVO;
import com.deyuanyun.pic.docmger.domain.DocPipeline;
import com.deyuanyun.pic.docmger.domain.DocPipelineExample;
import com.deyuanyun.pic.docmger.enumeration.DocEnum;
import com.deyuanyun.pic.docmger.mapper.DocPipelineExpandMapper;
import com.deyuanyun.pic.docmger.mapper.DocPipelineMapper;
import com.deyuanyun.pic.docmger.service.DocPipelineService;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import com.deyuanyun.pic.settings.mapper.base.AttachmentMapper;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  文档中心---管道相关service实现类
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-11-14
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
public class DocPipelineServiceImpl implements DocPipelineService {

    @Autowired
    private DocPipelineExpandMapper docPipelineExpandMapper;

    @Autowired
    private DocPipelineMapper docPipelineMapper;

    @Autowired
    private AttachmentMapper attachmentMapper;

    @Override
    public DocPipelineDTO selectByIdType(String id, String type) {
        List<DocPipelineDTO> docPipelineDTOs = docPipelineExpandMapper.queryPipelineInfo(new DocPipelineQueryVO().setId(id).setType(type));
        if(docPipelineDTOs == null || docPipelineDTOs.isEmpty()) {
            return null;
        }
        return docPipelineDTOs.get(0);
    }

    @Override
    public List<DocPipelineDTO> queryPipelineInfo(DocPipelineQueryVO queryVO) {
        return docPipelineExpandMapper.queryPipelineInfo(queryVO);
    }

    @Override
    public void deletePipelineInfo(String id) {
        DocPipelineExample docPipelineExample = new DocPipelineExample();
        docPipelineExample.createCriteria().andDeletedEqualTo((short) 0).andIdEqualTo(id);
        DocPipeline docPipeline = new DocPipeline();
        docPipeline.setDeleted((short) 1);
        docPipelineMapper.updateByExampleSelective(docPipeline, docPipelineExample);
        attachmentMapper.updateByBusinessId(id);
    }

    @Override
    public void updatePipelineInfo(DocPipeline docPipeline, String attachmentId) {
        String type = docPipeline.getType();
        DocEnum docEnum = DocEnum.valueOf(type);
        List<DocPipelineDTO> dtos = docPipelineExpandMapper.queryPipelineInfo(new DocPipelineQueryVO().setName(docPipeline.getName()).setType(type).
                setExcludedId(Lists.newArrayList(docPipeline.getId())));
        Asserts.assertTrue(dtos == null || dtos.isEmpty(), docPipeline.getName() + "名称已存在，请重新输入");

        DocPipelineExample docPipelineExample = new DocPipelineExample();
        docPipelineExample.createCriteria().andDeletedEqualTo((short) 0).andIdEqualTo(docPipeline.getId());
        docPipelineMapper.updateByExampleSelective(docPipeline, docPipelineExample);
        if(StringUtils.isNotEmpty(attachmentId)) {
            String id = docPipeline.getId();
            Attachment attachment = attachmentMapper.selectByPrimaryKey(attachmentId);
            if(attachment != null) {
                attachmentMapper.updateByBusinessId(id);
                attachment.setBusinessId(id);
                attachmentMapper.updateByPrimaryKeySelective(attachment);
            }
        }
    }

    @Override
    public void addPipelineInfo(DocPipeline docPipeline, String attachmentId) {
        String type = docPipeline.getType();
        DocEnum docEnum = DocEnum.valueOf(type);
        List<DocPipelineDTO> dtos = docPipelineExpandMapper.queryPipelineInfo(new DocPipelineQueryVO().setName(docPipeline.getName()).setType(type));
        Asserts.assertTrue(dtos == null || dtos.isEmpty(), docPipeline.getName() + "名称已存在，请重新输入");

        DomainInitUtil.initCreateDomain(docPipeline);
        docPipelineMapper.insertSelective(docPipeline);
        if(StringUtils.isNotEmpty(attachmentId)) {
            String id = docPipeline.getId();
            Attachment attachment = attachmentMapper.selectByPrimaryKey(attachmentId);
            if(attachment != null) {
                String businessId = attachment.getBusinessId();
                if (StringUtils.isNotEmpty(businessId)) {
                    DomainInitUtil.initCreateDomain(attachment);
                    attachment.setBusinessId(id);
                    attachmentMapper.insertSelective(attachment);
                } else {
                    DomainInitUtil.initUpdateDomain(attachment, "关联附件");
                    attachment.setBusinessId(id);
                    attachmentMapper.updateByPrimaryKeySelective(attachment);
                }
            }
        }
    }
}
